home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2346 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  981 b 

  1. Path: locutus.rchland.ibm.com!usenet
  2. From: pstaite@vnet.ibm.com
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: overloading []
  5. Date: 16 Jan 1996 22:18:04 GMT
  6. Organization: IBM OS/2 Device Driver Development  Rochester, MN
  7. Message-ID: <4dh86s$bfi@locutus.rchland.ibm.com>
  8. References: <4dgjbl$6i3@news1.goodnet.com>
  9. Reply-To: pstaite@vnet.ibm.com
  10. NNTP-Posting-Host: warpone.rchland.ibm.com
  11. X-Newsreader: IBM NewsReader/2 v1.2
  12.  
  13. In <4dgjbl$6i3@news1.goodnet.com>, geoff@goodguy (GEOFF CADIEN) writes:
  14.  
  15. >my problem is this. Given,
  16. >foo *f;
  17. >f = new foo;
  18. >Now the only way I can use the subscript operator is to call it like
  19. this
  20. >f->operator[](1) = 10;
  21. >Is this the only way?
  22.  
  23. No, you could do:
  24.  
  25. (*f)[ 1 ] = 10;
  26.  
  27. or what I would do:
  28.  
  29. foo& f( *new foo );
  30. f[ 1 ] = 10;
  31.  
  32. // later on:
  33.  
  34. delete &f;
  35.  
  36. Of course, you have to be careful of NULL comming back from new, or 
  37. catching the exception if it is a newer new ;-)
  38.  
  39.  
  40. Phil Staite, team OS/2
  41. internet: pstaite@vnet.ibm.com  internal: pstaite@rchland
  42.  
  43.